package com.cn.qgx428.lazydiary.ui.fragment


import android.os.Bundle
import android.os.CountDownTimer
import android.os.Handler
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.ui.activity.HomeActivity
import kotlinx.android.synthetic.main.fragment_ad.*
import org.jetbrains.anko.startActivity


/**
 * A simple [Fragment] subclass.
 */
class ADFragment : BaseFragment() {

    var isSkip  = false

    override fun getLayoutId() = R.layout.fragment_ad

    override fun initView() {
        var timer = MCountdownTimer(5000,1000)
        timer.start()

        //用户点击跳过广告，跳转主页并释放资源
        ad_countdownText.setOnClickListener({
            context.startActivity<HomeActivity>()
            timer.cancel()
            activity.finish()
        })
    }


    inner class MCountdownTimer(millisInFuture: Long, countDownInterval: Long) : CountDownTimer(millisInFuture, countDownInterval) {

        override fun onFinish() {
            // 计时器结束，跳转
            context.startActivity<HomeActivity>()
            activity.finish()
        }

        override fun onTick(p0: Long) {
            ad_countdownText.setText("跳过 " + (p0/1000).toString() + "s")
        }
    }
}// Required empty public constructor
